/*
 * Author:  Copyright (c) 2013 Toby
 * E-Mail:  smurffit@gmail.com
 * Web:     https://code.google.com/p/chrome-mfi-filter4kiva/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses/>.
 */
'use strict';

function sortTable(data, style)
{
	var resSorted = data.slice(0);

	switch (style)
	{
		case 1:
			if (lastSort === -1)
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}
					// subtraction doesn't work in JS... for some reason
					if (a.name > b.name)
					{
						return -1;
					}
					else if (a.name < b.name)
					{
						return 1;
					}
					return 0;
				});
				lastSort = 1;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}
					if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = -1;
			}
			break;
		case 2:
			if (lastSort === -2)
			{
				resSorted.sort(function(a, b)
				{
					return b.id - a.id;
				});
				lastSort = 2;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					return a.id - b.id;
				});
				lastSort = -2;
			}

			break;
		case 4:
			if (lastSort === -4)
			{
				resSorted.sort(function(a, b)
				{
					return a.rating - b.rating;
				});
				lastSort = 4;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					return b.rating - a.rating;
				});
				lastSort = -4;
			}

			break;
		case 8:
			if (lastSort === -8)
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}
					if (a.blocked && !b.blocked)
					{
						return 1;
					}
					else if (!a.blocked && b.blocked)
					{
						return -1;
					}

					if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = 8;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}

					if (a.blocked && !b.blocked)
					{
						return -1;
					}
					else if (!a.blocked && b.blocked)
					{
						return 1;
					}

					if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = -8;
			}

			break;
		case 16:
			if (lastSort === -16)
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}

					if (a.highlight && !b.highlight)
					{
						return 1;
					}
					else if (!a.highlight && b.highlight)
					{
						return -1;
					}

					if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = 16;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}

					if (a.highlight && !b.highlight)
					{
						return -1;
					}
					else if (!a.highlight && b.highlight)
					{
						return 1;
					}

					if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = -16;
			}

			break;
		case 32:
			if (lastSort === -32)
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}
					if (a.top > b.top)
					{
						return 1;
					}
					else if (a.top < b.top)
					{
						return -1;
					}

					if (a.religious > b.religious)
					{
						return 1;
					}
					else if (a.religious < b.religious)
					{
						return -1;
					}

					if (a.social > b.social)
					{
						return 1;
					}
					else if (a.social < b.social)
					{
						return -1;
					}

					else if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = 32;
			}
			else
			{
				resSorted.sort(function(a, b)
				{
					// Closed down
					if (a.rating === -1 && b.rating >= 0)
					{
						return 1;
					}
					else if (b.rating === -1 && a.rating >= 0)
					{
						return -1;
					}
					if (a.top > b.top)
					{
						return -1;
					}
					else if (a.top < b.top)
					{
						return 1;
					}
					if (a.religious > b.religious)
					{
						return -1;
					}
					else if (a.religious < b.religious)
					{
						return 1;
					}
					if (a.social > b.social)
					{
						return -1;
					}
					else if (a.social < b.social)
					{
						return 1;
					}
					else if (a.name > b.name)
					{
						return 1;
					}
					else if (a.name < b.name)
					{
						return -1;
					}
					return 0;
				});
				lastSort = -32;
			}

			break;
		default:
			console.log('No sort style provided.');
			break;
	}
	return resSorted;
}

/**
 * Selects / De-selects multiple items based on the given criteria Ids
 * @param filterId
 *            {number} id of the filter to apply (0 for none)
 * @param highlightId
 *            {number} id of the highlighter to apply (0 for none)
 * @param isSelect
 *            {boolean} whether this selects (true) or de-selects (false) the items
 */
function multiSelect(filterId, highlightId, isSelect)
{
	if (filterId > highlightId)
	{
		filter(filterId, isSelect);
	}
	else
	{
		highlight(highlightId, isSelect);
	}
}

function filter(filterId, isSelect)
{
	var element;
	switch (filterId)
	{
		case 1:// filter red
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].top === 1)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 2:// filter yellow
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].top === 2)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 3:// filter non-rated
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].top === 0)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;

		case 4:// filter Kiva non-rated
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 0)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;

		case 5:// filter Kiva one star rated
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 10 || res[i].rating === 15)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 6:// filter Kiva two star rated
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 20 || res[i].rating === 25)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 7:// filter religious
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].religious <= 2 && res[i].religious > 0)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 20: // (De-)select all filter
			for ( var i = 0, l = res.length; i < l; i++)
			{
				element = document.getElementById(res[i].id);
				element.checked = isSelect;
				updateItemManually(element);
			}
			break;
		case 21: // (De-)select kiva 3 star filter
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 30 || res[i].rating === 35)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 22: // (De-)select kiva 4 star filter
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 40 || res[i].rating === 45)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 23: // (De-)select kiva 5 star filter
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 50)
				{
					element = document.getElementById(res[i].id);
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
	}
}

function highlight(filterId, isSelect)
{
	var element;
	switch (filterId)
	{
		case 1:// highlight top
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].top === 4)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 2:// highlight high
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].top === 3)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 3:// highlight kiva 5
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 50)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 4:// highlight non-religious
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].religious >= 4)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 5:// highlight religious
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].religious <= 2 && res[i].religious > 0)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 6:// highlight social
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].social >= 3)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 20: // (De-)select all highlights
			for ( var i = 0, l = res.length; i < l; i++)
			{
				element = document.getElementById(res[i].id + 'd');
				element.checked = isSelect;
				updateItemManually(element);
			}
			break;
		case 21: // (De-)select no star highlights
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating <= 0)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
		case 22: // (De-)select 1 star highlights
			for ( var i = 0, l = res.length; i < l; i++)
			{
				if (res[i].rating === 10 || res[i].rating === 15)
				{
					element = document.getElementById(res[i].id + 'd');
					element.checked = isSelect;
					updateItemManually(element);
				}
			}
			break;
	}
}
